//
// Created on 2024/9/12.
//
#include "njs_rtc_client.h"
#include "api/video/video_render_manager.h"
#include "../rtc_log.h"

namespace ohosrtc {
NJSRtcClient::NJSRtcClient(std::string room_url, std::string room_id, std::function<void()> callback, bool use_native) : room_url_(room_url), room_id_(room_id), callback_(callback), use_native_(use_native) {}

void NJSRtcClient::Connect() {
  if (use_native_) {
    RTC_LOG << "use RTCClientNative channel";
    client_ = new ohosrtc::RTCClientNative();
  } else {
    client_ = new ohosrtc::RTCClient();
  }
  if (callback_) {
    client_->SetHangupCallback(callback_);
  }
  ohosrtc::RoomConnectionParameters params;
  params.room_url = room_url_;
  params.room_id = room_id_;
  client_->Connect(params);
}

void NJSRtcClient::Disconnect() {
	if (client_) {
		client_->Disconnect();
	}
  client_ = nullptr;
}

void NJSRtcClient::SetRemoteRender(std::string render_id) {
	if (client_) {
		auto render = webrtc::VideoRenderManager::GetInstance()->GetRender(render_id);
		if (render) {
			client_->SetRemoteRender(render);
    }
	}
}

void NJSRtcClient::StartScreenCast() {
	if (client_) {
		client_->StartScreenCast();
	}
}

void NJSRtcClient::StopScreenCast() {
	if (client_) {
		client_->StopScreenCast();
	}
}

void NJSRtcClient::FlipCamera(bool is_front) {
	if (client_) {
		client_->FlipCamera(is_front);
	}
}

void NJSRtcClient::MuteMic() {
	if (client_) {
		client_->MuteMic();
	}
}

void NJSRtcClient::UnMuteMic() {
	if (client_) {
		client_->UnMuteMic();
	}
}

void NJSRtcClient::SetLocalRender(std::string render_id) {
	if (client_) {
		auto render = webrtc::VideoRenderManager::GetInstance()->GetRender(render_id);
    RTC_LOG << "SetLocalRender render_id " << render_id << render;
		if (render) {
      RTC_LOG << "SetLocalRender inner " << render;
			client_->SetLocalRender(render);
    }
	}
}

}
